package com.printable;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

import javax.print.PrintService;


public class TestPrint implements Printable
{
	private static PrinterHelper printHelper = new PrinterHelper();

	/**
	 *   * @param Graphic指明打印的图形环境   * @param
	 * PageFormat指明打印页格式（页面大小以点为计量单位，1点为1英寸的1/72，1英寸为25.4毫米。A4纸大致为595×842点）   * @param
	 * pageIndex指明页号
	 **/
	public int print(Graphics gra, PageFormat pf, int pageIndex)
			throws PrinterException
	{
		int xStart = printHelper.getxStart();  
		int yStart = printHelper.getyStart();
		int yTextInc = printHelper.getyTextInc();
		int yTitleTextInc = printHelper.getyTitleTextInc();
		
		if (pageIndex > 0) {  
            return NO_SUCH_PAGE;  
        }  
  
        Graphics2D g2d = (Graphics2D) gra;  

        g2d.setFont(new Font("宋体", Font.BOLD, printHelper.getyTitleTextInc() -1));  
        g2d.drawString("欢迎光临", printHelper.getxCenter(g2d, "欢迎光临"), yStart);  
        yStart = printHelper.incTitleText();
        g2d.setFont(new Font("宋体", Font.PLAIN, printHelper.getyTextInc() -1));  
        g2d.drawString("-------------------------------------", xStart, yStart);  
        yStart = printHelper.incyText();
        g2d.drawString("台号：" + "17", xStart, yStart);  
        yStart = printHelper.incyText();
        g2d.drawString("单号：" + "OD12345678", xStart, yStart);
        yStart = printHelper.incyText();
        g2d.drawString("时间：" + "2016-01-20 24:30", xStart, yStart);
        yStart = printHelper.incyText();
        g2d.drawString("-------------------------------------", xStart, yStart);  
        yStart = printHelper.incyText();
        g2d.drawString("菜品", xStart, yStart);
        g2d.drawString("数量", xStart + printHelper.getxAvaliWidth() / 3 * 1, yStart);
        g2d.drawString("总价", xStart + printHelper.getxAvaliWidth() / 3 * 2, yStart);
        yStart = printHelper.incyText();
        g2d.drawString("青椒炒蛋", xStart, yStart);
        g2d.drawString("1", xStart + printHelper.getxAvaliWidth() / 3 * 1, yStart);
        g2d.drawString("15元", xStart + printHelper.getxAvaliWidth() / 3 * 2, yStart);
        yStart = printHelper.incyText();
        g2d.drawString("泡椒牛肉", xStart, yStart);
        g2d.drawString("2", xStart + printHelper.getxAvaliWidth() / 3 * 1, yStart);
        g2d.drawString("44元", xStart + printHelper.getxAvaliWidth() / 3 * 2, yStart);
        yStart = printHelper.incyText();
        g2d.drawString(printHelper.splitStringByWidth(g2d, "青蛙上树大杂烩", printHelper.getxAvaliWidth() / 3), xStart, yStart);
        g2d.drawString("2", xStart + printHelper.getxAvaliWidth() / 3 * 1, yStart);
        g2d.drawString("22元", xStart + printHelper.getxAvaliWidth() / 3 * 2, yStart);
        yStart = printHelper.incyText();
        g2d.drawString(printHelper.splitStringByWidth(g2d, "田螺", printHelper.getxAvaliWidth() / 3), xStart, yStart);
        g2d.drawString("1", xStart + printHelper.getxAvaliWidth() / 3 * 1, yStart);
        g2d.drawString("5元", xStart + printHelper.getxAvaliWidth() / 3 * 2, yStart);
        yStart = printHelper.incyText();
        //last
        g2d.drawString("6", xStart + printHelper.getxAvaliWidth() / 3 * 1, yStart);
        g2d.drawString("86元", xStart + printHelper.getxAvaliWidth() / 3 * 2, yStart);
        yStart = printHelper.incyText();
        g2d.drawString("-------------------------------------", xStart, yStart);  
  
        return PAGE_EXISTS;  
	}

	public static void test(PrintService service)
	{
		int height = printHelper.getHeight();  
		int width = printHelper.getWidth();
		  
        // 通俗理解就是书、文档  
        Book book = new Book();  
  
        // 打印格式  
        PageFormat pf = new PageFormat();  
        pf.setOrientation(PageFormat.PORTRAIT);  
  
        // 通过Paper设置页面的空白边距和可打印区域。必须与实际打印纸张大小相符。  
        Paper p = new Paper();  
        p.setSize(width, height);  
        p.setImageableArea(5, -20, width, height + 20);  
        pf.setPaper(p);  
  
        // 把 PageFormat 和 Printable 添加到书中，组成一个页面  
        book.append(new TestPrint(), pf);  
  
        // 获取打印服务对象  
        PrinterJob job = PrinterJob.getPrinterJob();  
        job.setPageable(book);  
        
        try {  
        	if ( service != null ) {
            	job.setPrintService(service);
            }
            job.print();  
        } catch (Exception e) {  
            System.out.println("================打印出现异常======");  
            System.out.println(e.getMessage());  
        } 
	}
	
	public static void initHelper() {
		printHelper.setWidth(230);
		printHelper.setxStart(7);
		printHelper.setyStart(20);
		printHelper.setyTextInc(12);
		printHelper.setyTitleTextInc(26);
		printHelper.setyCount(12);
		printHelper.setHeight(175 
				+ printHelper.getyTextInc() 
				+ printHelper.getyCount() * printHelper.getyTextInc() 
				+ 20);
	}
	
	public static void main(String[] args) {
		TestPrint.initHelper();
		TestPrint.test(null);
	}
}